home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / GETINFO.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  10KB  |  285 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getinfo.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTM_GETTOOLINFO"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. // Toolbar buttons. 
  108. //.................
  109. TBBUTTON tbButtons[] = 
  110.     { STD_FILENEW,  IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  111.     { STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  112.     { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  113.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0L, 0}, 
  114.     { VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  115.     { VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  116.     { VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  117.     { VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0}, 
  118. }; 
  119.  
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123. static HWND hToolWnd = NULL;
  124. static HWND hToolTip = NULL;
  125.  
  126.    switch( uMsg )
  127.    {
  128.       case WM_CREATE :
  129.               if ( !hToolWnd )
  130.               {
  131.   
  132.                  // Initialize the common control library
  133.                  // and create the status window and toolbar.
  134.                  //..........................................
  135.                  InitCommonControls();
  136.  
  137.                  hToolWnd = CreateToolbarEx( hWnd, 
  138.                                              WS_CHILD | WS_BORDER | 
  139.                                              WS_VISIBLE, 
  140.                                              102, 
  141.                                              11,
  142.                                              (HINSTANCE)HINST_COMMCTRL, 
  143.                                              IDB_STD_SMALL_COLOR, 
  144.                                              tbButtons, 4,
  145.                                              0, 0, 100, 30, sizeof(TBBUTTON) );
  146.  
  147.                  if ( hToolWnd )
  148.                  {
  149.                     TOOLINFO    ti;
  150.                     TBADDBITMAP tb;
  151.                     int index, stdidx;
  152.  
  153.                     // Add the system-defined view bitmaps.
  154.                     //.....................................
  155.                     tb.hInst = HINST_COMMCTRL;
  156.                     tb.nID = IDB_VIEW_SMALL_COLOR;
  157.                     stdidx = SendMessage(hToolWnd, TB_ADDBITMAP, 12, (LPARAM)&tb);
  158.  
  159.                     // Update the indices to the bitmaps.
  160.                     //...................................
  161.                     for (index = 4; index < 8; index++)
  162.                        tbButtons[index].iBitmap += stdidx;
  163.  
  164.                     // Add the view buttons.
  165.                     //......................
  166.                     SendMessage(hToolWnd, TB_ADDBUTTONS, 4, (LONG) &tbButtons[4]);
  167.  
  168.                     // Create tool tip control.
  169.                     //.........................
  170.                     hToolTip = CreateWindowEx( 0,
  171.                                                TOOLTIPS_CLASS, NULL, 
  172.                                                TTS_ALWAYSTIP,
  173.                                                CW_USEDEFAULT, 
  174.                                                CW_USEDEFAULT, 
  175.                                                CW_USEDEFAULT, 
  176.                                                CW_USEDEFAULT,
  177.                                                hWnd, NULL, 
  178.                                                hInst, NULL );
  179.  
  180.                     // Add the tools to the tool tip control.
  181.                     //.......................................
  182.                     ti.cbSize = sizeof( TOOLINFO );
  183.                     ti.hwnd   = hToolWnd;
  184.                     ti.hinst  = hInst;
  185.                     ti.uFlags = TTF_SUBCLASS;
  186.  
  187.                     for( index=0; index < 8; index++ )
  188.                     {
  189.                        if ( index == 3 )
  190.                           continue;
  191.  
  192.                        SendMessage( hToolWnd, TB_GETITEMRECT, 
  193.                                     index, (LPARAM)&ti.rect );
  194.  
  195.                        ti.uId      = tbButtons[index].idCommand;
  196.                        ti.lpszText = (LPTSTR)tbButtons[index].idCommand;
  197.  
  198.                        SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  199.                     }
  200.  
  201.                     // Set the tool tip control as 
  202.                     // part of the toolbar.
  203.                     //............................
  204.                     SendMessage( hToolWnd, TB_SETTOOLTIPS, 
  205.                                  (WPARAM)hToolTip, 0 );
  206.                  }
  207.               }
  208.               break;
  209.  
  210.       case WM_COMMAND :
  211.               switch( LOWORD( wParam ) )
  212.               {
  213.                  case IDM_NEW :
  214.                  case IDM_OPEN :
  215.                  case IDM_SAVE :
  216.                  case IDM_LARGEICON :
  217.                  case IDM_SMALLICON :
  218.                  case IDM_LISTVIEW :
  219.                  case IDM_REPORTVIEW :
  220.                         {
  221.                            TOOLINFO ti;
  222.                            char     szMsg[64], szTxt[32];
  223.                          
  224.                            ti.cbSize = sizeof( TOOLINFO );
  225.                            ti.hwnd   = hToolWnd;
  226.                            ti.uId    = LOWORD( wParam );
  227.                            SendMessage( hToolTip, TTM_GETTOOLINFO, 0, (LPARAM)&ti );
  228.  
  229.                            LoadString( ti.hinst, (UINT)ti.lpszText, szTxt, sizeof( szTxt ) );
  230.                            wsprintf( szMsg, 
  231.                                      "Tip text: %s   rect: %d %d %d %d", 
  232.                                      szTxt, 
  233.                                      ti.rect.left, ti.rect.top,
  234.                                      ti.rect.right, ti.rect.bottom );
  235.  
  236.                            MessageBox( hWnd, szMsg, lpszTitle, MB_OK | MB_ICONINFORMATION );
  237.                         }
  238.                         break;
  239.  
  240.                  case IDM_ABOUT :
  241.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  242.                         break;
  243.  
  244.                  case IDM_EXIT :
  245.                         DestroyWindow( hWnd );
  246.                         break;
  247.               }
  248.               break;
  249.       
  250.       case WM_DESTROY :
  251.               PostQuitMessage(0);
  252.               break;
  253.  
  254.       default :
  255.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  256.    }
  257.  
  258.    return( 0L );
  259. }
  260.  
  261.  
  262. LRESULT CALLBACK About( HWND hDlg,           
  263.                         UINT message,        
  264.                         WPARAM wParam,       
  265.                         LPARAM lParam)
  266. {
  267.    switch (message) 
  268.    {
  269.        case WM_INITDIALOG: 
  270.                return (TRUE);
  271.  
  272.        case WM_COMMAND:                              
  273.                if (   LOWORD(wParam) == IDOK         
  274.                    || LOWORD(wParam) == IDCANCEL)    
  275.                {
  276.                        EndDialog(hDlg, TRUE);        
  277.                        return (TRUE);
  278.                }
  279.                break;
  280.    }
  281.  
  282.    return (FALSE); 
  283. }
  284.